package com.geeksaga.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.geeksaga.dao.BbsDao;
import com.geeksaga.dao.BbsRateDao;
import com.geeksaga.domain.Bbs;
import com.geeksaga.domain.BbsRate;

/**
 * @author newbie
 * @since 0.1
 */
@Service
public class BbsRateServiceImpl implements BbsRateService {
    
    @Autowired protected BbsDao bbsDao;
    @Autowired protected BbsRateDao bbsRateDao;
    
    /* (non-Javadoc)
     * @see com.geeksaga.service.BbsRateService#add(com.geeksaga.service.BbsRateService)
     */
    public BbsRate addAfterCalculate(BbsRate bbsRate) throws DataAccessException {
        BbsRate rate = null;
        
        if(!bbsRateDao.exists(bbsRate)) {
            bbsRateDao.add(bbsRate);
            
            rate = bbsRateDao.calculate(bbsRate);
            
            Bbs bbs = new Bbs();
            bbs.setSid(bbsRate.getBbsSid());
            bbs.setGoodCnt(rate.getCount());
            bbs.setGoodSum(rate.getSum());
            
            bbsDao.updateCnt(bbs, Bbs.CNT_TYPE.RATE_PLUS_CNT);
        } else
            rate = bbsRateDao.calculate(bbsRate);
        
        return rate;
    }
}